import { getUserPermission } from '@/api/user'
import router, { resetRouter } from '@/router'

const state = {
  accessRoutes: []
}

const mutations = {
  SET_ACCESS_ROUTES: (state, accessRoutes) => {
    state.accessRoutes = accessRoutes
  }
}

const actions = {

  // 获取用户路由
  getAccessRoutes({ commit, state }) {

    return new Promise((resolve, reject) => {
      getUserPermission().then(response => {
        const { data: accessRoutes } = response

        // accessRoutes 必须是非空数组
        if (!accessRoutes || accessRoutes.length <= 0) {
          reject('用户权限路由: 必须是非空数组!')
        }

        commit('SET_ACCESS_ROUTES', accessRoutes)
        resolve(accessRoutes)
      }).catch(error => {
        reject(error)
      })
    })
  },


}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
